CREATE TABLE PMSUser
    (
      ID INT IDENTITY
             PRIMARY KEY ,
      UserName NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      LoginPwd NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      RealName NVARCHAR(50) NOT NULL
                            DEFAULT ( '' ) ,
      BirthDay DATETIME NOT NULL
                        DEFAULT ( '1900-01-01' ) ,
      Gender TINYINT NOT NULL
                     DEFAULT ( 0 ) ,
      Email NVARCHAR(50) NOT NULL
                         DEFAULT ( '' ) ,
      Mobile NVARCHAR(20) NOT NULL
                          DEFAULT ( '' ) ,
      TelePhone NVARCHAR(20) NOT NULL
                             DEFAULT ( '' ) ,
      HomeAddress NVARCHAR(200) NOT NULL
                                DEFAULT ( '' ) ,
      LastVisitTime DATETIME NOT NULL
                             DEFAULT ( GETDATE() ) ,
      LoginCount INT NOT NULL
                     DEFAULT ( 0 ) ,
      LoginIp NVARCHAR(50) NOT NULL
                           DEFAULT ( '' ) ,
      LoginBroswer NVARCHAR(50) NOT NULL
                                DEFAULT ( '' ) ,
      IsEnabled TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      IsDeleted TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OperateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    )


CREATE TABLE PMSRole
    (
      ID INT IDENTITY
             PRIMARY KEY ,
      OrganizeId INT NOT NULL
                     DEFAULT ( '' ) ,
      FullName NVARCHAR(50) NOT NULL
                            DEFAULT ( '' ) ,
      Categoty NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      Description NVARCHAR(200) NOT NULL
                                DEFAULT ( '' ) ,
      IsEnabled TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      IsDeleted TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OperateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    )
    
CREATE TABLE PMSUserRole
    (
      ID INT IDENTITY
             PRIMARY KEY ,
      UserID INT NOT NULL
                 DEFAULT ( 0 ) ,
      RoleID INT NOT NULL
                 DEFAULT ( 0 ) ,
      IsEnabled TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      IsDeleted TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OperateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    )

CREATE TABLE PMSModule
    (
      ID INT IDENTITY
             PRIMARY KEY ,
      ModuleName NVARCHAR(32) NOT NULL
                              DEFAULT ( '' ) ,
      ModuleUrl NVARCHAR(256) NOT NULL
                              DEFAULT ( '' ) ,
      IsMenu TINYINT NOT NULL
                     DEFAULT ( 0 ) ,
      IsEnabled TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      IsDeleted TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OperateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    )

CREATE TABLE PMSAction
    (
      ID INT IDENTITY
             PRIMARY KEY ,
      ModuleId INT NOT NULL
                   DEFAULT ( 0 ) ,
      ActionName NVARCHAR(32) NOT NULL
                              DEFAULT ( '' ) ,
      IsReadOnly TINYINT NOT NULL
                         DEFAULT ( 0 ) ,
      IsEnabled TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      IsDeleted TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OperateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    )
	
CREATE TABLE PMSCompany
    (
      ID INT IDENTITY
             PRIMARY KEY ,
      FullName NVARCHAR(50) NOT NULL
                            DEFAULT ( '' ) ,
      CompanyProfile NVARCHAR(MAX) NOT NULL
                                   DEFAULT ( '' ) ,
      HomePage NVARCHAR(200) NOT NULL
                             DEFAULT ( '' ) ,
      ContactAddress NVARCHAR(200) NOT NULL
                                   DEFAULT ( '' ) ,
      OfficePhone NVARCHAR(50) NOT NULL
                               DEFAULT ( '' ) ,
      OfficeFax NVARCHAR(50) NOT NULL
                             DEFAULT ( '' ) ,
      Postalcode NVARCHAR(20) NOT NULL
                              DEFAULT ( '' ) ,
      Email NVARCHAR(50) NOT NULL
                         DEFAULT ( '' ) ,
      LogoImage NVARCHAR(200) NOT NULL
                              DEFAULT ( '' ) ,
      IsEnabled TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      IsDeleted TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OpetateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    )
	
CREATE TABLE PMSOrganization
    (
      ID INT IDENTITY
             PRIMARY KEY ,
      ParentId INT NOT NULL
                   DEFAULT ( 0 ) ,
      CompanyId INT NOT NULL
                    DEFAULT ( 0 ) ,
      FullName NVARCHAR(50) NOT NULL
                            DEFAULT ( '' ) ,
      ShortName NVARCHAR(50) NOT NULL
                             DEFAULT ( '' ) ,
      OfficePhone NVARCHAR(50) NOT NULL
                               DEFAULT ( '' ) ,
      OfficeFax NVARCHAR(50) NOT NULL
                             DEFAULT ( '' ) ,
      ADDRESS NVARCHAR(200) NOT NULL
                            DEFAULT ( '' ) ,
      DESCRIPTION NVARCHAR(200) NOT NULL
                                DEFAULT ( '' ) ,
      IsEnabled TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      IsDeleted TINYINT NOT NULL
                        DEFAULT ( 0 ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OpetateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    ) 

	
CREATE TABLE PMSSequenceGenerator
    (
      ID TINYINT PRIMARY KEY
                 IDENTITY ,
      FullName NVARCHAR(50) NOT NULL
                            DEFAULT ( '' ) ,
      Prefix NVARCHAR(10) NOT NULL
                          DEFAULT ( '' ) ,
      DataValue INT NOT NULL
                    DEFAULT ( '' ) ,
      DESCRIPTION NVARCHAR(200) NOT NULL
                                DEFAULT ( '' ) ,
      Operator NVARCHAR(32) NOT NULL
                            DEFAULT ( '' ) ,
      OpetateTime DATETIME NOT NULL
                           DEFAULT ( GETDATE() )
    )

   
IF OBJECT_ID('PROC_GetSequenceCode', 'p') IS NOT NULL 
    DROP PROCEDURE PROC_GetSequenceCode
    go
CREATE PROCEDURE PROC_GetSequenceCode ( @Datakey NVARCHAR(255) )
AS 
    BEGIN 
        DECLARE @MaxValue INT
        BEGIN TRANSACTION
        SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
        SELECT  @MaxValue = DataValue
        FROM    dbo.PMSSequenceGenerator WITH ( NOLOCK )
        WHERE   FullName = @Datakey
        IF @MaxValue > 0 
            BEGIN
                SET @MaxValue = @MaxValue + 1
                UPDATE  dbo.PMSSequenceGenerator
                SET     DataValue = @MaxValue
                WHERE   FullName = @Datakey
            END
        ELSE 
            BEGIN
                SET @MaxValue = 1
                INSERT  INTO dbo.PMSSequenceGenerator
                        ( FullName, DataValue )
                VALUES  ( @DataKey, @MaxValue )
            END
        SELECT  @MaxValue
        IF @@ERROR <> 0 
            ROLLBACK TRANSACTION
        COMMIT TRANSACTION
    END